package com.sanjeet.homecontrol;


import java.io.IOException;
import java.net.UnknownHostException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class EventHandler implements OnClickListener, OnCheckedChangeListener {
	
	Context context;
	
	public static TcpSocketHandler connection;

	public EventHandler(Context homeControl) {
		
		this.context=homeControl;
	}

	public void onClick(View e) {
		
		
		/*switch (e.getId()) {
	    case R.id.button1:
	    	
			try {
				connection= new TcpSocketHandler("192.168.1.221",5678);
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				
	    	Toast.makeText(this.context, "Button connect", Toast.LENGTH_LONG).show();
	        break;
	    case R.id.button2:
	    	connection.disconnect();
	    	Toast.makeText(this.context, "Button disconnect", Toast.LENGTH_LONG).show();
	       break;
	    case R.id.button3:
	    	connection.send("20");
	    	for(int i =0;i!=10000;i++);
    		String json=connection.reciever();
			JSONObject jObject;
			try {
				jObject = new JSONObject(json);
				String aJsonString = jObject.getString("temp");
				TextView temperature= (TextView)((Activity)this.context).findViewById(R.id.textView3);
				temperature.setText(aJsonString);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
	    	break;
	    }   */
	
		
		
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		/*switch(buttonView.getId())
		{
			
		 	case R.id.switch1:
		    	this.checkSwitch(isChecked, "111", "110");
		        break;
		    case R.id.switch2:
		    	this.checkSwitch(isChecked, "121", "120");
		       break;
		
		}*/
		
		
		
	}
	
	private void checkSwitch(boolean checked,String paramchecked,String unchecked)
	{
				boolean on = checked;
			    
			    if (on) {
			    		connection.send(paramchecked);
			    		//String json=connection.reciever();
			    		//Toast.makeText(this.context, json, Toast.LENGTH_SHORT).show();
			    } else {
			    		connection.send(unchecked);
			    		//String json=connection.reciever();
			    		//Toast.makeText(this.context, json, Toast.LENGTH_SHORT).show();
			    }
	}
}
